﻿ using Company.Plc;
using Company.Shell.Interfaces;
using Company.Shell.Models;


namespace Company.Shell.Services
{
    public class HardwareManager : IHardwareManager
    {
        public bool Initialized { get; private set; }
        private IPlc Plc { get; }

        public HardwareManager(IPlc plc)
        {
            Plc = plc;
        }
        public void Close()
        {
            Plc.Close();
        }

        public async Task<InitializeResult> InitalizeAsync()
        {
            if (Initialized)
            {
                throw new InvalidOperationException("重复初始化硬件");
            }

            return await Task.Run(() =>
            {
                InitializeResult result = new InitializeResult();
                bool success = Plc.Init(new PlcConfig
                {
                    IP = "127.0.0.1"
                });
                if (success)
                {
                    Initialized = true;
                    result.Success = success;
                    result.Message = "硬件初始化成功";
                }
         

                return result;
            });
        }
    }
}
